package com.sun.StudentInfo.utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ConflictUtil {

    // 判断是否有交集
    public static boolean isConflict(Set<Integer> set1, Set<Integer> set2) {
        set1.retainAll(set2); // 取两个集合的交集，结果放在set1中
        if (set1.size() > 0) {
            return true; // set1大于0，则说明有交集
        } else {
            return false;
        }
    }

    // 获取上课周的集合
    public static Set<Integer> parseWeek(String week) {
        Set<Integer> weeks = new HashSet<>();
        for (String s : week.split("，")) {
            int index1 = s.indexOf("第"); // 这个一般是0
            int index2 = s.indexOf("周");
            String substring = s.substring(index1 + 1, index2); // 截取得到：1-10
            String[] split = substring.split("-");
            if (split.length == 2) {
                int startWeek = Integer.parseInt(split[0]); // 开始周
                int endWeek = Integer.parseInt(split[1]); // 结束周
                for (int i = startWeek; i <= endWeek; i++) {
                    weeks.add(i); // 将所有要上课的周放入set集合中
                }
            }
        }
        return weeks;
    }


    // 判断课程节次是否有交集， map1是已选课程的，map2待选课程的
    public static boolean isConflict(Map<Integer, Set<Integer>> map1, Map<Integer, Set<Integer>> map2) {
        Set<Integer> weekSet1 = map1.keySet(); // 已选课程周几集合
        Set<Integer> weekSet2 = map2.keySet(); // 待选课程周几集合

        // 判断每周上课是周几集合是否有交集，有的话再判断具体节次是否有冲突
        if (isConflict(weekSet1, weekSet2)) {
            for (Integer week1 : weekSet1) {
                for (Integer week2 : weekSet2) {
                    // 当周几相同时，判断节次是否有冲突
                    if (week1.equals(week2)) {
                        Set<Integer> selections1 = map1.get(week1); // 已选课程节次
                        Set<Integer> selections2 = map2.get(week2); // 待选课程节次

                        boolean conflict = isConflict(selections1, selections2);
                        // 判断节次是否有冲突，只有这种情况下返回true，其他都返回false
                        if (conflict) {
                            return true;
                        }
                    }

                }
            }
        }
        return false;
    }

    // 获取每天上课的节次放入map中，key是周几，value是节次结合
    public static Map<Integer, Set<Integer>> parseTime(String time) {
        Map<Integer, Set<Integer>> times = new HashMap<>();
        for (String s : time.split("，")) {
            String[] split = s.split("：");
            String week = split[0];
            int weekNum = parseWeekly(week); // 上课周

            String courseSelection = split[1];
            int index = courseSelection.indexOf("节");
            String substring = courseSelection.substring(0, index); // 截取到”3-4“格式
            String[] selectionStr = substring.split("-");
            int startSelection = Integer.parseInt(selectionStr[0]); // 第几节课开始
            int endSelection = Integer.parseInt(selectionStr[1]); // 第几节课结束

            Set<Integer> selections = new HashSet<>(); // 每天上课的节次放入set集合
            for (int i = startSelection; i <= endSelection; i++) {
                selections.add(i); // 将所有要上课的周放入set集合中
            }
            times.put(weekNum, selections);
        }
        return times;
    }

    // 将星期字符串转换为数字
    public static int parseWeekly(String week) {
        switch (week) {
            case "周一":
                return 1;
            case "周二":
                return 2;
            case "周三":
                return 3;
            case "周四":
                return 4;
            case "周五":
                return 5;
            case "周六":
                return 6;
            case "周日":
                return 7;
        }
        return 1;
    }
}
